package com.qms.repast.ui.fragment;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.qms.repast.R;
import com.qms.repast.entity.MessageEvent;
import com.qms.repast.weidgets.calender.GregorianLunarCalendarView;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author 泅渡者
 * @date 2018/9/14 0014
 * @describe: 日历选择弹窗
 */
@SuppressLint("WrongConstant")
public class CalendarDialog extends DialogFragment {
	@BindView(R.id.iv_cancle)
	TextView ivCancle;
	@BindView(R.id.iv_sure)
	TextView ivSure;
	@BindView(R.id.calendar_view)
	GregorianLunarCalendarView calendarView;

	@Nullable
	@Override
	public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
		//设置背景透明
		getDialog().getWindow().setBackgroundDrawableResource(R.drawable.draw_black_transparent_half);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog (Bundle savedInstanceState) {
		Dialog dialog = new Dialog(getActivity());
		// 设置Content前设定
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.layout_calender);
		// 外部点击取消
		dialog.setCanceledOnTouchOutside(true);
		// 设置宽度为屏宽, 靠近屏幕底部。
		Window window = dialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.gravity = Gravity.BOTTOM;
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		window.setAttributes(lp);
		ButterKnife.bind(this, dialog);
		calendarView.init();
		return dialog;
	}

	@Override
	public void onDestroyView () {
		super.onDestroyView();
	}

	@OnClick({R.id.iv_cancle, R.id.iv_sure})
	public void onViewClicked (View view) {
		switch (view.getId()) {
			case R.id.iv_cancle:
				EventBus.getDefault().post(new MessageEvent(104, "CalendarDialog"));
				break;
			case R.id.iv_sure:
				GregorianLunarCalendarView.CalendarData calendarData = calendarView.getCalendarData();
				Calendar calendar = calendarData.getCalendar();
				String showToast = "Gregorian : " + calendar.get(Calendar.YEAR) + "-"
						+ (calendar.get(Calendar.MONTH) + 1) + "-"
						+ calendar.get(Calendar.DAY_OF_MONTH);
				EventBus.getDefault().post(new MessageEvent(105, showToast));
				break;
			default:
				break;
		}
	}
}
